﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.COMM;
using Mang24k99.Business.COMM;
using Wuqi.Webdiyer;

namespace Mang24k99.Web.COMM
{
    public partial class KtCommCodeType : System.Web.UI.Page
    {
        string _AspNetName = "CustomInfoHTML";
        public string AspNetCustomInfoHTML
        {
            get
            {
                return Common.ViewStateHelper.GetString(this.ViewState, _AspNetName);
            }
            set
            {
                Common.ViewStateHelper.SetViewState(this.ViewState, value, _AspNetName);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnCancel.Attributes.Add("onclick", "return Cancel()");
            if (!IsPostBack)
            {
                this.AspNetCustomInfoHTML = this.AspNetPager1.CustomInfoHTML;
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                BindAspNetPager();
                BinDate();
            }
        }
        //// <summary>
        /// 新增或者修改一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
          
            //当Session值为Insert，代表是新增
            if (Session["Action"].ToString()=="Insert")
            {
                if (CodeValidate()==false)
                {
                    return;
                }
                if (NameValidate()==false)
                {
                    return;
                }
                MKtCommCodeType codetype = new MKtCommCodeType();
                codetype.TYPE_CODE = Int32.Parse(txtCode.Text.Trim());
                codetype.TYPE_NAME = this.txtName.Text.Trim();
                codetype.TYPE_SPEC = Int32.Parse(ddlSpec.SelectedValue);
                codetype.IS_VALIDATE = Int32.Parse(ddlIsvalidate.SelectedValue);
                codetype.TYPE_REMARK = this.txtRemark.Text;
                codetype.INPUT_TIME = DateTime.Now;
                bool result = BKtCommCodeType.Save(codetype);
                if (result == true)
                {
                    string strMsg = "新增成功!";
                    Common.MessageBox.Show(this.Page, strMsg);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    ClearPage();
                    BindAspNetPager();
                    BinDate();
                }
                else
                {
                    string strMsg = "新增失败!";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            } 
            if (Session["Action"].ToString()=="Update")
            {
                if (txtCode.Text.Trim() != Session["Code"].ToString())
                {
                    if (CodeValidate() == false)
                    {
                        return;
                    }
                }
                if (txtName.Text.Trim() != Session["Name"].ToString())
                {
                    if (NameValidate() == false)
                    {
                        return;
                    }
                }
                MKtCommCodeType codetype = new MKtCommCodeType();
                codetype = (MKtCommCodeType)MKtCommCodeType.Select(Int32.Parse(txtCode.Text.Trim()));
                codetype.TYPE_CODE = Int32.Parse(txtCode.Text.Trim());
                codetype.TYPE_NAME = this.txtName.Text.Trim();
                codetype.TYPE_SPEC = Int32.Parse(ddlSpec.SelectedValue);
                codetype.IS_VALIDATE = Int32.Parse(ddlIsvalidate.SelectedValue);
                codetype.TYPE_REMARK = this.txtRemark.Text;
                codetype.MODIFY_TIME = DateTime.Now;
                bool result = BKtCommCodeType.Update(codetype);
                if (result == true)
                {
                    string strMsg = "修改成功!";
                    Common.MessageBox.Show(this.Page, strMsg);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    this.txtCode.ReadOnly = false;
                    ClearPage();
                    BinDate();
                }
                else
                {
                    string strMsg = "修改失败!";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            }        
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["Action"] != null)
            {
                if (Session["Action"].ToString() == "Insert")
                {
                    ClearPage();
                }
                if (Session["Action"].ToString() == "Update")
                {
                    if (Session["Code"]!=null)
                    {
                        InItDate(Convert.ToInt32(Session["Code"].ToString()));
                    }
                }
            }
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="type"></param>
        public void InItDate(int code)
        {
            MKtCommCodeType codetype = MKtCommCodeType.Select(code);
            txtCode.Text = codetype.TYPE_CODE.ToString();
            txtName.Text = codetype.TYPE_NAME;
            txtRemark.Text = codetype.TYPE_REMARK;
            ddlIsvalidate.SelectedValue = codetype.IS_VALIDATE.ToString();
            ddlSpec.SelectedValue = codetype.TYPE_SPEC.ToString();
        }
        /// <summary>
        /// 转换是否有效字段的字符串格式
        /// </summary>
        /// <param name="isValidate"></param>
        /// <returns></returns>
        public string GetStatus(int isValidate)
        {
            if (isValidate == 1)
            {
                return "使用";
            }
            else
            {
                return "禁用";
            }
        }
        /// <summary>
        ///转换编码分类格式：1——二维表，2——层次表
        /// </summary>
        /// <param name="typeSpec"></param>
        /// <returns></returns>
        public string GetSpec(int typeSpec)
        {
            if (typeSpec==1)
            {
                return "二维表";
            } 
            else
            {
                return "层次表";
            }
        }
        public void ClearPage()
        {
            this.txtCode.Text = "";
            this.txtName.Text = "";
            this.txtRemark.Text = "";
            this.ddlIsvalidate.SelectedValue = "0";
            this.ddlSpec.SelectedValue = "1";
        }

        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BinDate()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int maxRecord = aspNetPager.PageSize;
            int startRecord = aspNetPager.StartRecordIndex - 1;
            object dataSource = BKtCommCodeType.FindALLSliced(startRecord, maxRecord);
            this.grdList.DataSource = dataSource;
            this.grdList.DataBind();
        }


        /// <summary>
        /// Binds the ASP net pager.(绑定分页控件)
        /// </summary>
        private void BindAspNetPager()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int RecordCount = BKtCommCodeType.GetCount();
            aspNetPager.RecordCount = RecordCount;
            aspNetPager.CustomInfoHTML = string.Format(AspNetCustomInfoHTML, aspNetPager.PageCount);
        }

        /// <summary>
        /// Handles the PageChanged event of the AspNetPager1 control.(列表改变时候)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindAspNetPager();
            BinDate();
        }


        public void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MKtCommCodeType codetype = new MKtCommCodeType();
            string code = this.grdList.Rows[e.RowIndex].Cells[2].Text;
            codetype = (MKtCommCodeType)MKtCommCodeType.Select(Convert.ToInt32(code));
            Boolean result = BKtCommCodeType.Delete(codetype);
            if (result == true)
            {
                string strMsg = "删除成功!";
                Common.MessageBox.Show(this.Page, strMsg);
                BindAspNetPager();
                BinDate();
            }
            else
            {
                string strMsg = "删除失败!";
                Common.MessageBox.Show(this.Page, strMsg);
            }
        }

        /// <summary>
        /// 点击编辑列，选择一条记录进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.grdList.SelectedIndex;
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            if (grdList.Rows[index].Cells[2].Text != "")
            {
                this.txtCode.ReadOnly = true;
                this.txtCode.Text = grdList.Rows[index].Cells[2].Text;
                Session["Code"] = grdList.Rows[index].Cells[2].Text;
            }
            if (grdList.Rows[index].Cells[3].Text != "")
            {
                this.txtName.Text = grdList.Rows[index].Cells[3].Text;
                Session["Name"] = grdList.Rows[index].Cells[3].Text;
            }

            if (grdList.Rows[index].Cells[6].Text != "" && grdList.Rows[index].Cells[6].Text!="&nbsp;")
            {
                this.txtRemark.Text = grdList.Rows[index].Cells[6].Text;
            }
            GridViewRow gvr = grdList.Rows[index];
            Label lab = (Label)gvr.FindControl("lab");
            Label labspec = (Label)gvr.FindControl("labspec");
            this.ddlIsvalidate.ClearSelection();
            this.ddlSpec.ClearSelection();
            this.ddlSpec.Items.FindByText(labspec.Text).Selected = true;
            this.ddlIsvalidate.Items.FindByText(lab.Text).Selected = true;
            Session["Action"] = "Update";
            Session["ID"] = grdList.Rows[index].Cells[1].Text;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.txtCode.ReadOnly = false;
            Session["Action"] = "Insert";
        }

        //代码编码有效性验证
        public bool CodeValidate()
        {
            string strValidate = BKtCommCodeType.CodeValidate(this.txtCode.Text.Trim());
            if (strValidate != "success")
            {
                string strMsg = strValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        public bool NameValidate()
        {
            //验证代码名称有效性
            string strNameValidate = BKtCommCodeType.NameValidate(this.txtName.Text.Trim());
            if (strNameValidate != "success")
            {
                string strMsg = strNameValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            ClearPage();
        }
    }
}
